/*
 * Represents an Item object
 * @author Kevin
 * @version 1.0
 * @date Winter 2014
 */

package model;

import java.util.Deque;
import java.util.LinkedList;

/**
 * Represents an Item object
 * 
 * @author Kevin
 * @version 1.0
 * @date Winter 2014
 */
public class Item {

	/** Item number will start from this point */
	private static final int BASE_ITEM_NUMBER = 100000;

	/** Tracks number of items that have been created */
	private static int num_of_items = 0;

	/** The auction containing this item */
	private Auction my_auction;

	/** Item number */
	private int my_item_number;

	/** Number of items per unit */
	private int my_quantity;

	/** Description of the item */
	private String my_description;

	/** String to the path of the photo file */
	private String my_photo;

	/** History of bids. "Top" bid will be highest and will win the item */
	private Deque<Bid> my_bid_history = new LinkedList<Bid>();

	private String my_itemName;

	private String my_itemSize;

	private String my_startingBid;

	private String my_donor;

	private String my_itemStorage;

	private String my_condition;

	/* ************************************************ */

	/**
	 * Main constructor with all parameters.
	 * 
	 * @pre the_auction != null
	 * 
	 * @param the_auction
	 *            An auction.
	 * @param the_description
	 *            The description of the item.
	 * @param the_photo
	 *            The location of the photo of the item.
	 * @param the_quantity
	 *            The quantity of the item.
	 */
	public Item(final Auction the_auction, final String the_description,
			final String the_photo, final int the_quantity) {
		my_auction = the_auction;
		my_item_number = BASE_ITEM_NUMBER + num_of_items;
		num_of_items++;
		my_quantity = the_quantity;
		setMyDescription(the_description);
		setMyPhoto(the_photo);
	}

	/**
	 * Constructor with no photo.
	 * 
	 * @pre the_auction != null
	 * 
	 * @param the_auction
	 *            An auction.
	 * @param the_description
	 *            The description of the item.
	 * @param the_quantity
	 *            The quantity of the item.
	 */
	public Item(final Auction the_auction, final String the_description,
			final int the_quantity) {
		this(the_auction, the_description, "", the_quantity);
	}

	/**
	 * Constructor with no description or photo.
	 * 
	 * @pre the_auction != null
	 * 
	 * @param the_auction
	 *            An auction.
	 * @param the_quantity
	 *            The quantity of the item.
	 */
	public Item(final Auction the_auction, final int the_quantity) {
		this(the_auction, "", "", the_quantity);
	}
	
	public Item(Auction the_auction, String the_itemName, String the_quantity,
			String the_startingBid, String the_donor, String the_size,
			String the_storage, String the_condition, String comments) {
		
		my_item_number = BASE_ITEM_NUMBER + num_of_items++;
		my_auction = the_auction;
		my_itemName = the_itemName;
		my_quantity = Integer.parseInt(the_quantity);
		setMy_startingBid(the_startingBid);
		my_donor = the_donor;
		my_itemSize = the_size;
		my_itemStorage = the_storage;
		my_condition = the_condition;
		my_description = comments;
	}
	
	/* ************************************************ */

	/**
	 * This method is used ONLY if the program is shut down and restarted, so
	 * the num of items can be saved and resumed from the same point.
	 * 
	 * @pre i > -1
	 * 
	 * @param i
	 *            the num of items that previously existed.
	 */
/*	public void setNumOfItems(int i) {
		num_of_items = i;
	}*/

	/**
	 * Sets the number of items per unit. (Six beers per one six-pack unit)
	 * 
	 * @pre the_quantity > -1
	 * 
	 * @param the_quantity
	 *            the number of items per unit.
	 */
	public void setQuantity(int the_quantity) {
		my_quantity = the_quantity;
	}

	/**
	 * Run at start of system launch to reset for File I/O. In a real system
	 * this would not be happening.
	 */
	public static void reset() {
		num_of_items = 0;
	}

	/**
	 * Returns the number of items per unit.
	 * 
	 * @return the my_quantity
	 */
	public int getQuantity() {
		return my_quantity;
	}

	/**
	 * Returns the auction number.
	 * 
	 * @return the my_auction_number
	 */
	public int getMyAuctionNumber() {
		return my_auction.getAuctionNumber();
	}

	/**
	 * Returns the Auction.
	 * 
	 * @return the my_auction
	 */
	public Auction getAuction() {
		return my_auction;
	}

	/**
	 * Returns the item number.
	 * 
	 * @return the my_item_number
	 */
	public int getMyItemNumber() {
		return my_item_number;
	}

	/**
	 * Returns the description.
	 * 
	 * @return the my_description
	 */
	public String getMyDescription() {
		return my_description;
	}

	/**
	 * Sets the description.
	 * 
	 * @param my_description
	 *            the my_description to set
	 */
	public void setMyDescription(String my_description) {
		this.my_description = my_description;
	}

	/**
	 * Returns the photo.
	 * 
	 * @return the my_photo
	 */
	public String getMyPhoto() {
		return my_photo;
	}

	/**
	 * Sets my photo.
	 * 
	 * @param my_photo
	 *            the my_photo to set
	 */
	public void setMyPhoto(String my_photo) {
		this.my_photo = my_photo;
	}

	/**
	 * Add a bid to the item; will not add if it is lower than current high bid.
	 * 
	 * @param b
	 *            the Bid to be added
	 */
	public void addBid(Bid b) {
		if (highestBid() == null) {
			my_bid_history.add(b);
		} else if (b.getBid() > highestBid().getBid()) {
			my_bid_history.add(b);
		}
	}

	/**
	 * Gives the current high bid on the item.
	 * 
	 * @return null if empty, highest bid if exists
	 */
	public Bid highestBid() {
		Bid result = new Bid(this, null, 0);
		if (!my_bid_history.isEmpty()) {
			// Since it is a stack, last bid must be the highest
			result = my_bid_history.getLast();
		}
		return result;
	}

	/**
	 * Removes the requested bid.
	 * 
	 * @param cancel_me
	 *            bid to be removed.
	 */
	public void removeBid(Bid cancel_me) {
		my_bid_history.remove(cancel_me);
	}

	/**
	 * Returns the bid history of an item.
	 * 
	 * @return the my_bid_history
	 */
	public Deque<Bid> get_Bid_History() {
		return my_bid_history;
	}

	/**
	 * @return the my_itemName
	 */
	public String getMy_itemName() {
		return my_itemName;
	}

	/**
	 * @return the my_itemSize
	 */
	public String getMy_itemSize() {
		return my_itemSize;
	}

	/**
	 * @return the my_donor
	 */
	public String getMy_donor() {
		return my_donor;
	}

	/**
	 * @return the my_itemStorage
	 */
	public String getMy_itemStorage() {
		return my_itemStorage;
	}

	/**
	 * @return the my_condition
	 */
	public String getMy_condition() {
		return my_condition;
	}

	/**
	 * @return the my_startingBid
	 */
	public String getMy_startingBid() {
		return my_startingBid;
	}

	/**
	 * @param my_startingBid the my_startingBid to set
	 */
	public void setMy_startingBid(String my_startingBid) {
		this.my_startingBid = my_startingBid;
	}

	/**
	 * Returns a String representation of the Item object.
	 * 
	 * @return a String representation.
	 */
	public String toString() {
		return my_item_number + " " + my_description;
	}

	public String toStringTwo() {
		return my_auction.getAuctionNumber() + "~" + my_description + "~"
				+ my_photo + "~" + my_quantity;
	}
}
